package com.nebula.controller.management;

import com.alibaba.fastjson.JSON;
import com.nebula.common.Const;
import com.nebula.common.PageResult;
import com.nebula.common.Result;
import com.nebula.controller.BaseController;
import com.nebula.entity.Icon;
import com.nebula.entity.Role;
import com.nebula.service.IIconService;
import com.nebula.service.IRoleService;
import com.nebula.service.IUserService;
import com.nebula.service.impl.IconServiceImpl;
import com.nebula.service.impl.RoleServiceImpl;
import com.nebula.service.impl.UserServiceImpl;
import com.nebula.vo.IconVo;
import com.nebula.vo.UserVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author CcccX
 * @since 2020-08-11 22:24
 */
@WebServlet(name = "IconSettingController", urlPatterns = "/manage/iconSetting")
public class IconSettingController extends BaseController {

    private final IIconService iconService = new IconServiceImpl();

    private final IRoleService roleService = new RoleServiceImpl();

    public String index(HttpServletRequest request, HttpServletResponse response) {
        String navId = request.getParameter("id");
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        IUserService userService = new UserServiceImpl();
        if (userService.checkNavRole(user.getRoleId(), navId)) {
            return Const.FORWARD + Const.DEFAULT_URL + "website/icon-setting.jsp";
        } else {
            return Const.FORWARD + "role-error.jsp";
        }
    }

    public String findByPage(HttpServletRequest request, HttpServletResponse response) {
        String currPage = request.getParameter("page");
        String pageSize = request.getParameter("rows");
        if (StringUtils.isBlank(currPage)) {
            currPage = Const.PAGE;
        }
        if (StringUtils.isBlank(pageSize)) {
            pageSize = Const.SIZE;
        }
        PageResult<IconVo> pageResult = iconService.findByPage(Integer.parseInt(currPage), Integer.parseInt(pageSize));
        Map<String, Object> map = new HashMap<>(2);
        map.put("total", pageResult.getTotalCount());
        map.put("rows", pageResult.getList());
        return Const.JSON + JSON.toJSONString(map);
    }

    public String findAll(HttpServletRequest request, HttpServletResponse response) {
        return Const.JSON + JSON.toJSONString(iconService.findAll());
    }

    public String findById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        if (StringUtils.isBlank(id)) return null;

        IconVo iconVo = iconService.findById(id);
        return Const.JSON + JSON.toJSONString(Result.createBySuccess(iconVo));
    }

    public String save(HttpServletRequest request, HttpServletResponse response) {
        String iconCls = request.getParameter("iconCls");
        String desc = request.getParameter("desc");
        if (StringUtils.isBlank(iconCls) || StringUtils.isBlank(desc)) {
            return null;
        }
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        if (user == null) {
            return null;
        }
        Role role = roleService.findById(user.getRoleId());
        Icon icon = new Icon();
        icon.setIcon(iconCls);
        icon.setUser(user.getUsername());
        icon.setInstructions(desc);
        icon.setRole(role.getRole());
        Date date = new Date();
        icon.setUpdatedTime(date);
        icon.setCreatedTime(date);
        try {
            iconService.insert(icon);
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("添加成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("添加失败"));
        }
    }

    public String update(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String desc = request.getParameter("desc");
        if (StringUtils.isBlank(id) || StringUtils.isBlank(desc)) return null;
        try {
            Icon icon = new Icon();
            icon.setId(Integer.parseInt(id));
            icon.setInstructions(desc);
            iconService.update(icon);
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("保存失败"));
        }

    }

    public String delete(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        if (StringUtils.isBlank(id)) return null;
        try {
            iconService.delete(Integer.parseInt(id));
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("删除失败"));
        }
    }
}
